﻿using System;
using Castle.DynamicProxy;
using Turquoise.Logging.Entities;
using Turquoise.Logging.Log4Net;

namespace Turquoise.Logging.Aspects.CastleWindsor
{
    public class LogInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var attribute = Attribute.GetCustomAttribute(invocation.Method, typeof(TurquoiseLogAttribute), false) as TurquoiseLogAttribute;
            if (attribute == null)
            {
                return;
            }
            try
            {
                invocation.Proceed();
            }
            catch (Exception exception)
            {
                var turquoiseLogger = new Log4NetLogManager();
                turquoiseLogger.Log(invocation.Method.DeclaringType, exception.Message, attribute.LogOptions);
                throw exception;
            }
        }
    }
}
